#include "serverHead/threadPool.h" 
#include "serverHead/sys.h"
#include "serverHead/sql_command.h"

// 工人线程
void* workerThreadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;
    train_t* train = (train_t*)calloc(1, sizeof(train_t));
    while(1){
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->taskQueue.queueSize == 0){
            // 未接收到命令，且当前没有任务
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        //正常执行
        bzero(train ,sizeof(train_t));
        printf("工人线程得到任务！\n");
        train = threadPool->taskQueue.pFront->train;
        workerExecute(train, threadPool->path);
        printf("火车长度%d\n",(int)strlen(train->data));
        printf("火车2%s\n", train->data);
        sendTrain(threadPool->netfd, train);
        taskDeQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);
    }
}

// 小组长线程，管理线程池
void *leaderThreadFunc(void *arg){
    createInfo* info = (createInfo*)arg;
    threadPool_t* threadPool = (threadPool_t*)calloc(1, sizeof(threadPool_t));
    threadPoolInit(threadPool, info->workerNum, info->netfd);
    makeWorker(threadPool);

    train_t* train = (train_t*)calloc(1, sizeof(train_t));
    ERROR_CHECK(train, NULL, "server_train");

    int epfd = epoll_create(1);
    epollAdd(epfd, info->netfd);
    struct epoll_event readyset[1];
    while(1){
        epoll_wait(epfd, readyset, 1,-1);
        if(readyset[0].data.fd == info->netfd){
            pthread_mutex_lock(&threadPool->taskQueue.mutex);
            bzero(train, sizeof(train_t));
            recvTrain(info->netfd, train);
            printf("order:%d\n data_len:%d\n data:%s\n", train->order,train->dataLen,train->data);
            taskEnQueue(&threadPool->taskQueue, train);
            printf("小组长下发了一个任务！\n");
            pthread_cond_signal(&threadPool->taskQueue.cond);
            pthread_mutex_unlock(&threadPool->taskQueue.mutex);
        }
    }
}

int makeWorker(threadPool_t *threadPool){
    for(int i = 0; i < threadPool->threadnum; ++i){
        pthread_create(&threadPool->tidArr[i], NULL, workerThreadFunc, (void*)threadPool);
    }
    return 0;
}

int workerExecute(train_t* train, char* path){
    printf("worker name = %s\n", train->data);
    printf("path = %s\n", path);
    int ret;
    char buf[4096] = {0};//buf存ls的结果
    switch(train->order){
    case ORDER_SIGNUP:
        printf("signup!\n");        
        ret = mySignup(train->data);
        bzero(train, sizeof(train_t));
        if(ret != 0){
            strcpy(train->data, "Signup failed!\n");
        }else{
            strcpy(train->data, "Signup succeeded!\n");
        }
        break;
    case ORDER_SIGNIN:
        printf("signin!\n");
        ret = mySignin(train->data, path);
        bzero(train, sizeof(train_t));
        if(ret != 0){
            strcpy(train->data, "Signin failed!\n");
        }else{
            strcpy(train->data, "Signin succeeded!\n");
        }
        printf("登陆后的路径为%s\n", path);
        break;
    case ORDER_SIGNDEL:
        printf("signdel!\n");
        mySigndel(path);
        bzero(train, sizeof(train_t));
        strcpy(train->data, "Signdel succeeded!\n");
        break;
    case ORDER_CD:
        printf("cd！\n");
        ret = myCd(train->data, &path);
        bzero(train, sizeof(train_t));
        if(ret == 0 ){
            strcpy(train->data, path);
        }else{
            strcpy(train->data, "Cd failed!\n");
        } 
        break;
    case ORDER_LS:
        printf("ls!\n");
        myLs(path, buf);
        bzero(train, sizeof(train_t));
        strcpy(train->data, buf);
        printf("火车%s\n", train->data);
        break;
    case ORDER_PUTS:
        printf("puts\n");
        //TODO
        break;
    case ORDER_GETS:
        printf("gets!\n");
        //TODO
        break;
    case ORDER_REMOVE:
        printf("remove!\n");
        myRemove(path, train->data);
        break;
    case ORDER_PWD:
        printf("pwd!\n");
        bzero(train, sizeof(train_t));
        strcpy(train->data, path);
        break;
    case ORDER_MKDIR:
        printf("mkdir!\n");
        myMkdir(path, train->data);
        bzero(train, sizeof(train_t));
        strcpy(train->data, "error");
        break;
    case ORDER_RMDIR:
        printf("rmdir!\n");
        myRmdir(path, train->data);
        break;
    default:
        printf("error!\n");
    }
    return 0;
}
